const {getDmg,reduceResistanceK,lvSuppressK,objValueAdd } = require("../libs/apis.js")

const WEAPONS = require("./weapons.js");

const CLIPDATA = {
    fixedAtt: 19,
    reaction : 23,
    attRate: 5.8, 
    hpRate: 5.8, 
    fixedhp: 299,
    criticalRate: 3.9,
    criticalStrike: 7.8,
    defence: 23,
    defRate: 7.3,
    charging: 7.3
}

const CONFIG = {
    chance: 20,
    your_lv: 90,
    enemy_lv: 90,
    clips_main: {
        criticalStrike: 62.2,
        dmgRate: 46.6,
        attRate: 46.6
    },
    clips: ["reaction", "attRate", "criticalRate", "criticalStrike"],
    clips_initial: [
        ["reaction", "attRate", "criticalRate", "criticalStrike"],
        ["reaction", "attRate", "criticalRate", "criticalStrike"],
        ["fixedAtt", "reaction", "criticalRate", "criticalStrike"],
        ["reaction", "attRate", "criticalRate", "criticalStrike"],
        ["fixedAtt", "reaction", "attRate", "criticalRate"]
    ],
    reactionK : 2,
    reactionDmgAdd: 0,
    buffs: [  
        { dmgRate: 15, tianfu1dmg: 30}, // 水4
        // { attRate: 18}, // 余响
        { attRate: 20 + 25 + 20, fixedAtt: 1202, aDmgRate:16, dmgRate:40, jk:40 } // 万班香
    ]
}
// 余响 冬极白星 1015306 弓藏743622  绝弦717858
// 水4 冬极白星 1001394 弓藏716766  绝弦698449
let charaters = {
    units : {
        name: "公子",
        reaction: 0,
        charging: 0,
        defRate: 0,
        defence: 815,
        dmgZhi:0,
        hp:13103,
        base: 301,  
        tianfu1dmg: 0,
        dmgRate: 28.8,  
        criticalRate: 5,    
        criticalStrike: 50,
        skills: {
            a: [
                function(data){},
            ]
        }
    },
}

calc(calc_clips_initial());

function calc(normal) {
    let charactersDmg = [];
    let count = 0;
    Object.values(WEAPONS).forEach(weapon => {
        if(weapon.weapontype == "bow") {
            charactersDmg.push(getDMG([charaters.units,weapon,{hp:4832,fixedAtt:311,...CONFIG.clips_main},normal], (data,second) => {
                count++;
                data = objValueAdd([data, ...CONFIG.buffs]);
                data.hp = data.hp*(1 + (data.hpRate||0)/100);
                if(weapon.skill) { weapon.skill(data); }
    
                let rea = { reaction:data.reaction, reactionQ:CONFIG.reactionK, reactionDmgAdd:CONFIG.reactionDmgAdd }
                // 12 级 e
                let attack = data.base *(1 + data.attRate/100) + data.fixedAtt;
    
                let ADmg = getDmg({
                    data: {
                        ...data,
                        dmgRate: data.dmgRate + data.tianfu1dmg + (data.aDmgRate||0),
                        // dmgZhi: attack * 0.35 * 4
                    },
                    rate: 1.3+0.768+0.823+1.114+1.185+1.093+0.7+0.745 // (断流爆触发 1/1.5s) 1.44+0.8769+0.939+1.2707+1.3522+1.2474
                }).qw * reduceResistanceK(10) * lvSuppressK(CONFIG.your_lv, CONFIG.enemy_lv); //
    
                let AZDmg = getDmg({
                    data: {
                        ...data,
                        dmgRate: data.dmgRate + data.tianfu1dmg
                    },
                    rate: 1.358+1.6238 // (断流爆触发 1/1.5s)
                }).qw * reduceResistanceK(10) * lvSuppressK(CONFIG.your_lv, CONFIG.enemy_lv); //
    
                let DlDmg = getDmg({
                    data:{...data, dmgRate:data.dmgRate},
                    rate:  1.358,
                }).qw * reduceResistanceK(10) * lvSuppressK(CONFIG.your_lv, CONFIG.enemy_lv); //
    
                let QDmg = getDmg({
                    data:{...data, dmgRate:data.dmgRate},
                    rate:  6.81,
                    rea
                }).qw * reduceResistanceK(10) * lvSuppressK(CONFIG.your_lv, CONFIG.enemy_lv); //
    
                let QDmgBJ = getDmg({
                    data:{...data, dmgRate:data.dmgRate},
                    rate:  6.81,
                    rea
                }, rea).bj * reduceResistanceK(10) * lvSuppressK(CONFIG.your_lv, CONFIG.enemy_lv); //
    
                if(data.phyRate) { // 物理特效伤害
                    data.phyDmg = getDmg({data:{...data, dmgRate:0}, rate:data.phyRate}).qw * reduceResistanceK(10) * lvSuppressK(CONFIG.your_lv, CONFIG.enemy_lv);  // 
                }
    
                let point = ADmg * 4 + AZDmg * 0 + QDmg + 6 * DlDmg  + (data.phyDmg||0) * 0
                // let point = QDmgBJ
                // QDmgBJ
                return {point,data, second, attack, msg:{}}
            }))
        }
        
    })
    
    charactersDmg.sort((a,b)=>{
        return (b.point)-(a.point)
    });
    console.log(charactersDmg[0]);
    console.log(charactersDmg.map((v, n)=>{
        return  `(${n})得分: ${v.data.weaponname} : ${v.point.toFixed(0)}`
    }));
    console.log(count);
}

function getDMG(dataarr, getRet){
    let chance = CONFIG.chance;
    let _ret = [];
    for (let index = 0; index <= chance; index++) {
        let c1 = index; 
        for (let index2 = 0; index2 <= chance - index; index2++) {
            let c2 = index2;
            for (let index3 = 0; index3 <= chance-index-index2; index3++) {
                let c3 = index3;
                let c4 = chance - index - index2 - index3;
                let cs = [c1, c2, c3, c4];
                var tmpdata = {};
                CONFIG.clips.forEach((v, i) => {
                    tmpdata[v] = CLIPDATA[v] * cs[i]
                })
                _ret.push(tmpdata);
            }
        }
    }
    _ret = _ret.map(second => {
        let panel = objValueAdd([...dataarr, second]);
        return  getRet(panel, second)
    })
    _ret.sort((b,a)=> a.point-b.point)
    return _ret[0]
}

function calc_clips_initial () {
    let clips = CONFIG.clips_initial.map(v => {
        let tmp = {};
        v.forEach(v2 => {
            tmp[v2] = CLIPDATA[v2]
        })
        return tmp
    })
    let data = {}
    clips.forEach(item => {
        for (const key in item) {
            data[key] = data[key]? (data[key]+item[key]) : item[key]
        }
    })
    return data
}
